package com.mall.manager.authority.service;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import org.springframework.web.util.WebUtils;

import com.mall.untils.DateUtil;
import com.mall.untils.Page;
import com.mall.untils.StringUtil;

/**
 * 用户信息列表
 * @author liugb
 * @date 2016 7 7
 */
@Service
public class UsersService {
	
	@Resource
    private JdbcTemplate jdbcTemplate;
	  
	private Logger logger = LoggerFactory.getLogger(UsersService.class);
	/**
	 * 用户列表分页方法
	 * @param request
	 * @return
	 */
	public Page manager_tusers_list(HttpServletRequest request) {
		Page page = null;
	    Map<String,Object> paraMap = WebUtils.getParametersStartingWith(request, "");
	    int pageNo = MapUtils.getIntValue(paraMap, "currentpageno");
	    String name = MapUtils.getString(paraMap, "name");
	    String str = "";
	    if(!StringUtils.isEmpty(name)){
	    	str = StringUtil.getByteToString(name);	    	
	    }
	    int pageSize = 10;
    	try {
 	        if (pageNo == 0){
 	        	pageNo = 1;
 	        }
 	       String sql = "";
 	        if(StringUtils.isEmpty(str)){
 	        	sql = "select count(*) from t_users ";
 	        }else{
 	        	sql = "select count(*) from t_users where 1=1 and name like '%"+ str +"%'"; 
 	        }	        
 	        int total = this.jdbcTemplate.queryForObject(sql, Integer.class);
 	        int startIndex = Page.getStartOfPage(pageNo, pageSize);
 	        if(StringUtils.isEmpty(str)){
 	        	sql = "select * from t_users order by id desc limit " + startIndex + "," + pageSize; 	   
 	        }else{
 	        	sql = "select * from t_users where 1=1 and name like '%"+ str +"%' order by id desc limit " + startIndex + "," + pageSize; 	        	
 	        }
 	        List<Map<String, Object>>  queryForList = this.jdbcTemplate.queryForList(sql);
 	        page = new Page(startIndex, total, pageSize, queryForList);
 	        return page;
 	    } catch (Exception e) {
 	        this.logger.info(e.toString());
 	        return null;
 	    }
	}
	/**
	 * 用户删除方法
	 * @param paraMap
	 * @return
	 */
	public int manager_del_tusers(Map<String, Object> paraMap) {
		int id = MapUtils.getIntValue(paraMap, "id");
		try {
			String sql="delete from t_users where id= "+ id;
			jdbcTemplate.execute(sql);
//			int update = jdbcTemplate.update(sql,id);
			return 1;
		} catch (Exception e) {
			e.printStackTrace();
			return 0;
		}
	}
	/**
	 * 用户修改方法
	 * @param paraMap
	 * @return
	 */
	public int manager_update_tusers(Map<String, Object> paraMap) {
		String account = MapUtils.getString(paraMap, "account");
		String password = MapUtils.getString(paraMap, "password");
		String name = MapUtils.getString(paraMap, "name");
		String age = MapUtils.getString(paraMap, "age");
		String sex = MapUtils.getString(paraMap, "sex");
		String phone = MapUtils.getString(paraMap, "phone");
		String email = MapUtils.getString(paraMap, "email");
		String id = MapUtils.getString(paraMap, "id");
		try {
			String sql = "update t_users set account=?,password=?,name=?,age=?,sex=?,phone=?,email=? where id = ?";
			int update = jdbcTemplate.update(sql,account,password,name,age,sex,phone,email,id);
			return update;
		} catch (Exception e) {
			e.printStackTrace();
			return 0;
		}
	}
    /**
     * 用户信息添加数据
     * @param paraMap
     * @return
     */
	public int manager_insert_tusers(Map<String, Object> paraMap) {
		String account = MapUtils.getString(paraMap, "account");
		String password = MapUtils.getString(paraMap, "password");
		String name = MapUtils.getString(paraMap, "name");
		int age = MapUtils.getIntValue(paraMap, "age");
		int sex = MapUtils.getIntValue(paraMap, "sex");
		String phone = MapUtils.getString(paraMap, "phone");
		String email = MapUtils.getString(paraMap, "email");
		String time =  DateUtil.getTodayString();
		try {
			String sql="insert into t_users(account,password,name,age,sex,phone,email,create_time,update_time) values(?,?,?,?,?,?,?,?,?)";
			int update = jdbcTemplate.update(sql,account,password,name,age,sex,phone,email,time,time);
			return update;
		} catch (Exception e) {
			e.printStackTrace();
			return 0;
		}
	}
	/**
	 * 登陆查询用户信息
	 * @param type_flag_id
	 * @return
	 */
	public Map<String, Object> manager_find_user(Map<String, Object> paraMap) {
		String account = MapUtils.getString(paraMap, "account");
		String password = MapUtils.getString(paraMap, "password");
		try {
			logger.info("当前登录用账号account："+account+"，密码password："+password);
			if(StringUtils.isNotEmpty(account)&&StringUtils.isNotEmpty(password)){
				String sql="select * from t_users where account=? and password=?";
				return jdbcTemplate.queryForMap(sql,account,password);
			}
		} catch (Exception e) {
			logger.info("登录出现异常："+e.toString());
		}
		return null;
	}
	/**
	 * 根据id查询方法
	 * @param userId
	 * @return
	 */
	public Map<String, Object> findById(Integer userId){
		String sql = "select * from t_users where id=?";
		Map<String, Object> map = jdbcTemplate.queryForMap(sql, userId);
		return map;
	}
	/**
	 * 根据登陆账号查询用户信息
	 * @param account
	 * @return
	 */
	public Map<String,Object> findUsers(String account){
		String sql = "select * from t_users where account =?";
		Map<String, Object> map = jdbcTemplate.queryForMap(sql, account);
		return map;
	}
	/**
	 * 根据id查询用户所对应的角色
	 * @param paraMap
	 * @return
	 */
	public int findUserPart(Map<String, Object> paraMap){
		int id = MapUtils.getIntValue(paraMap, "id");
		String sql = "select count(*) from t_user t,t_user_part p where t.id = p.user_id and t.id=?";
		int flag = jdbcTemplate.queryForObject(sql, Integer.class, id);
		return flag;
	}
	
}
